package com.example.mylearn.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class reflectExample {


    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {

        //1.直接通过类名获取类
        Class<TargetObject> targetClass = TargetObject.class;
        final TargetObject targetObject = (TargetObject) targetClass.newInstance();


        Method[] declaredMethods = targetClass.getDeclaredMethods();

        for (Method declaredMethod : declaredMethods) {
            System.out.println(declaredMethod.getName());
        }

        Method publicMethod = targetClass.getDeclaredMethod("publicMethod", String.class);
//        publicMethod.setAccessible(true);
        publicMethod.invoke(targetObject, "you");


        Field value = targetClass.getDeclaredField("value");
        value.setAccessible(true);
        value.set(targetObject, "learn");


        Method privateMethod = targetClass.getDeclaredMethod("privateMethod");
        privateMethod.setAccessible(true);
        privateMethod.invoke(targetObject);


    }










}
